Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding in more methods for Array #1749

Merged
merged 1 commit into from
Sep 4, 2019

Conversation

Pauan
Copy link
Contributor

@Pauan Pauan commented Sep 3, 2019

Adds in the following methods:

  • new_with_length to create an Array with a specific length
  • get to retrieve the element at an index
  • set to set the element at an index
  • delete to delete the element at an index

Also adds a FromIterator impl, which means it's now easy to create an Array from an Iterator by using collect:

vec![ ... ].iter().collect::<js_sys::Array>()

@Pauan Pauan mentioned this pull request Sep 3, 2019
@Pauan
Copy link
Contributor Author

Pauan commented Sep 3, 2019

The only controversial thing is the FromIterator impl. In particular, there's a few different ways it could be implemented:

  • Accept AsRef<JsValue> (which is what this PR does)

  • Accept Into<JsValue>

  • Accept something else?

Using Into<JsValue> is good, it allows for some native types like &str, u32, etc. But it doesn't work for certain things, like Closure

Using AsRef<JsValue> is very good, but it does require the user to manually call JsValue::from for things like &str, u32, etc. This isn't a big deal, they can just do .map(JsValue::from)

Another issue with AsRef<JsValue> is that it doesn't (currently) work with #[wasm_bindgen] structs, because they don't implement AsRef<JsValue>. Is there any technical reason they can't impl AsRef<JsValue>?

@alexcrichton
Copy link
Contributor

Thanks! This all seems great to me, and I'm fine with AsRef chosen here.

Is there any technical reason they can't impl AsRef?

Currently they don't actually have the JsValue on-hand anywhere in the struct to implement AsRef with, we don't actually modify the struct we just generate a wrapper around the struct. I saw a comment from you about this on another thread though that I haven't gotten to, so I may be missing something as well.

@Pauan
Copy link
Contributor Author

Pauan commented Sep 3, 2019

Currently they don't actually have the JsValue on-hand anywhere in the struct to implement AsRef with, we don't actually modify the struct we just generate a wrapper around the struct.

Ahhh, right. So in that case if a user wants to send a struct they'll have to use .map(JsValue::from), just like with native types. That's fine, after all Rust structs are a native type.

@alexcrichton alexcrichton merged commit ba85275 into rustwasm:master Sep 4, 2019
@alexcrichton
Copy link
Contributor

👍

@Pauan Pauan deleted the array-from-iter branch September 5, 2019 02:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants